package org.zjt.spark.traffic

import org.apache.commons.pool2.impl.GenericObjectPoolConfig
import redis.clients.jedis.JedisPool

object RedisClient extends Serializable {
  val redisHost = "centos"
  val redisPort = 6379
  val redisTimeout = 30000
  lazy val pool = new JedisPool(new GenericObjectPoolConfig(), redisHost, redisPort, redisTimeout)

  // TODO: 利用懒加载销毁 jedis pool链接 
  lazy val hook = new Thread {
    override def run = {
      println("Execute hook thread: " + this)
      pool.destroy()
    }
  }

  // TODO: jvm关闭默认会调用hook.run方法，销毁jedis链接
  sys.addShutdownHook(hook.run)
}




object RedisTest extends App{
  val jedis = RedisClient.pool.getResource
  jedis.set("zhang","san")
  println(jedis.get("zhang"))
  jedis.close()
}
